#!/bin/bash

DELAY=2
#PREFERENCE_RANDOM_MAC_ADDRESS="mac_addr=1\npreassoc_mac_addr=1\ngas_rand_mac_addr=1"
PREFERENCE_RANDOM_MAC_ADDRESS=""
CONFIG_PATH="/etc/wpa_supplicant/"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_NORMAL="echo -en \\033[0;39m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_SEPARATOR="echo -en \\033[1;34m"
SETCOLOR_MENU="echo -en \\033[1;36m"

print_menu() {
  $SETCOLOR_NORMAL
  echo "\nWelcome to EWI"
  $SETCOLOR_SEPARATOR
  echo "\n:::::::::::::::::::::::::::::::::::::::::::::::::::"
  $SETCOLOR_MENU
  echo "\n1 - Scan wifi"
  echo "\n2 - Connect to wifi and generate config file"
  echo "\n3 - Connect to wifi (do not generate config file)"
  echo "\n4 - Disconnect to wifi"
  echo "\n5 - Print wifi configs"
  echo "\n6 - Delete wifi config"
  echo "\n0 - Exit"
  $SETCOLOR_SEPARATOR
  echo "\n:::::::::::::::::::::::::::::::::::::::::::::::::::"
  $SETCOLOR_NORMAL
}

kill_process() {
  # Kill it if wifi is work
  PROCESS_DHCLIENT=$(pgrep "dhclient")
  PROCESS_WPA_SUPPLICANT=$(pgrep "wpa_supplicant")

  if [[ "$PROCESS_WPA_SUPPLICANT" == [0-9]* ]]; then
    kill -15 "$PROCESS_WPA_SUPPLICANT"
  fi

  if [[ "$PROCESS_DHCLIENT" == [0-9]* ]]; then
    kill -15 "$PROCESS_DHCLIENT"
  fi
}

scan_wifi() {
  # Up wireless and scan to wifi points
  ip link set $WIRELESS up
  WIFI_LIST=$(iw $WIRELESS scan | grep -o "SSID: .*" | grep -o " .*")
  echo "$WIFI_LIST"
}

generateConfigFile() {
  # Save config file
  local CONFIG_FILE=$(wpa_passphrase $WIFI_SSID $WIFI_PASSWORD)
  echo -e "$CONFIG_FILE" > "$CONFIG_FILE_PATH" &&
  echo -e $PREFERENCE_RANDOM_MAC_ADDRESS >> "$CONFIG_FILE_PATH"
}

print_wifi_adapters() {
  clear
  for ((i=0; i < ${#WIFI_ADAPTERS__ARRAY[@]}; i++)) do
    echo "\n::::::::::::::::::::::::::::::::::"
    echo "\n    ${i} ${WIFI_ADAPTERS__ARRAY[$i]}"
  done
  echo "\n::::::::::::::::::::::::::::::::::"
}

print_wifi() {
  clear
  for ((i=0; i < ${#WIFI_LIST__ARRAY[@]}; i++)) do
    echo "\n::::::::::::::::::::::::::::::::::"
    echo "\n    ${i} ${WIFI_LIST__ARRAY[$i]}"
  done
  echo "\n::::::::::::::::::::::::::::::::::"
}

print_wifi_ssid() {
  clear
  for ((i=0; i < ${#WIFI_SSID_ARRAY[@]}; i++)) do
    echo "\n::::::::::::::::::::::::::::::::::"
    echo "\n    ${i} ${WIFI_SSID_ARRAY[$i]}"
  done
  echo "\n::::::::::::::::::::::::::::::::::"
}

connect() {
  wpa_supplicant -B -i $WIRELESS -c "$CONFIG_FILE_PATH"
  dhclient "$WIRELESS"
}

USER=$(whoami)
if [[ "$USER" != "root" ]]; then
  echo -e "\n::::::::::::::::::::::::::::::::::"
  echo -e "\nPlease run as root user"
  echo -e "\n::::::::::::::::::::::::::::::::::"
  sleep $DELAY
  exit
fi

while true; do
  # Print menu
  clear
  echo -e $(print_menu)
  read -p "SELECT OPTION: " option

  # if's OPTIONS
  if [[ "$option" == 0 ]]; then
    clear
    break
  elif [[ "$option" == 1 ]]; then
    # Kill wifi if it work
    $(kill_process)

    # Get list wireless
    WIFI_ADAPTERS__ARRAY=$(iw dev | grep -io "wl.*" | fmt)
    WIFI_ADAPTERS__ARRAY=($WIFI_ADAPTERS__ARRAY)

    # Print list wireless and select it
    while true; do
      echo -e $(print_wifi_adapters)
      read -p "Select to wireless: " SELECT_WIRELESS


      if [[ $SELECT_WIRELESS == [0-9] ]]; then
        WIRELESS=${WIFI_ADAPTERS__ARRAY[$SELECT_WIRELESS]}
      else
        $SETCOLOR_FAILURE
        echo "This adaprer is not =("
        sleep $DELAY
        $SETCOLOR_NORMAL
        continue
      fi

      if [[ "$WIRELESS" == "" ]] ; then
        $SETCOLOR_FAILURE
        echo "This adaprer is not =("
        $SETCOLOR_NORMAL
        sleep $DELAY
        continue
      fi
      break
    done

    # Scan wifi points
    WIFI_LIST__ARRAY=$(scan_wifi | fmt -w 2500)
    WIFI_LIST__ARRAY=($WIFI_LIST__ARRAY)

    # Print list wifi points
    echo -e $(print_wifi)
    read -p "Press 'Enter' to continue: "
    continue
  elif [[ "$option" == 2 ]]; then
    # Kill wifi if it work
    $(kill_process)

    # Get list wireless
    WIFI_ADAPTERS__ARRAY=$(iw dev | grep -io "wl.*" | fmt)
    WIFI_ADAPTERS__ARRAY=($WIFI_ADAPTERS__ARRAY)

    # Print list wireless and select it
    while true; do
      echo -e $(print_wifi_adapters)
      read -p "Select to wireless: " SELECT_WIRELESS

      if [[ $SELECT_WIRELESS == [0-9] ]]; then
        WIRELESS=${WIFI_ADAPTERS__ARRAY[$SELECT_WIRELESS]}
      else
        $SETCOLOR_FAILURE
        echo "This adaprer is not =("
        $SETCOLOR_NORMAL
        sleep $DELAY
        continue
      fi

      if [[ "$WIRELESS" == "" ]] ; then
        $SETCOLOR_FAILURE
        echo "This adaprer is not =("
        $SETCOLOR_NORMAL
        sleep $DELAY
        continue
      fi

      break
    done

    # Scan wifi points
    WIFI_LIST__ARRAY=$(scan_wifi | fmt -w 2500)
    WIFI_LIST__ARRAY=($WIFI_LIST__ARRAY)

    # Print list wifi points
    while true; do
      echo -e $(print_wifi)
      read -p "Select to wifi: " SELECT_WIFI

      if [[ "$SELECT_WIFI" == [0-9] ]]; then
        WIFI_SSID=${WIFI_LIST__ARRAY["$SELECT_WIFI"]}
      else
        $SETCOLOR_FAILURE
        echo "This wifi ssid is not =("
        $SETCOLOR_NORMAL
        sleep $DELAY
        continue
      fi

      if [[ "$WIFI_SSID" == "" ]]; then
        $SETCOLOR_FAILURE
        echo "This wifi ssid is not =("
        $SETCOLOR_NORMAL
        sleep $DELAY
        continue
      fi

      break
    done

    # Get password to wifi
    while true; do
      clear
      read -p "Password: " WIFI_PASSWORD

      if (( ${#WIFI_PASSWORD} < 8 )); then
        $SETCOLOR_FAILURE
        echo "Password must be 8..63 characters"
        $SETCOLOR_NORMAL
        sleep $DELAY
        continue
      fi
      break
    done


    # Generate config and connect
    CONFIG_FILE_PATH="/etc/wpa_supplicant/${WIFI_SSID}.conf"
    $(generateConfigFile)
    clear
    $SETCOLOR_SUCCESS
    echo "Connection ${WIFI_SSID}..."
    echo -e $(connect)
    break
  elif [[ "$option" == 3 ]]; then
    # Kill wifi if it work
    $(kill_process)

    # Get list wireless
    WIFI_ADAPTERS__ARRAY=$(iw dev | grep -io "wl.*" | fmt)
    WIFI_ADAPTERS__ARRAY=($WIFI_ADAPTERS__ARRAY)

    # Print list wireless and select it
    while true; do
      echo -e $(print_wifi_adapters)
      read -p "Select to wireless: " SELECT_WIRELESS

      if [[ $SELECT_WIRELESS == [0-9] ]]; then
        WIRELESS=${WIFI_ADAPTERS__ARRAY[$SELECT_WIRELESS]}
      else
        $SETCOLOR_FAILURE
        echo "This adaprer is not =("
        $SETCOLOR_NORMAL
        sleep $DELAY
        continue
      fi

      if [[ "$WIRELESS" == "" ]] ; then
        $SETCOLOR_FAILURE
        echo "This adaprer is not =("
        $SETCOLOR_NORMAL
        sleep $DELAY
        continue
      fi
      break
    done

    WIFI_SSID_ARRAY=$(ls -w 1 "$CONFIG_PATH" | grep -oi ".*[^.conf$]" | fmt -w 2500)
    WIFI_SSID_ARRAY=($WIFI_SSID_ARRAY)

    # Print list wifi points
    while true; do
      echo -e $(print_wifi_ssid)
      read -p "Select to wifi: " SELECT_WIFI_SSID

      if [[ "$SELECT_WIFI_SSID" == [0-9] ]]; then
        WIFI_SSID=${WIFI_SSID_ARRAY["$SELECT_WIFI_SSID"]}
      else
        $SETCOLOR_FAILURE
        echo "This wifi ssid is not =("
        $SETCOLOR_NORMAL
        sleep $DELAY
        continue
      fi

      if [[ "$WIFI_SSID" == "" ]]; then
        $SETCOLOR_FAILURE
        echo "This wifi ssid is not =("
        $SETCOLOR_NORMAL
        sleep $DELAY
        continue
      fi

      CONFIG_FILE_PATH="$CONFIG_PATH${WIFI_SSID}.conf"
      break
    done

    # connect
    clear
    $SETCOLOR_SUCCESS
    echo "Connection $WIFI_SSID..."
    echo -e $(connect)
    break
  elif [[ "$option" == 4 ]]; then
    $(kill_process)
    $SETCOLOR_SUCCESS
    echo "Disconnect successfully"
    sleep $DELAY
  elif [[ "$option" == 5 ]]; then
    WIFI_SSID_ARRAY=$(ls -w 1 "$CONFIG_PATH" | grep -oi ".*[^.conf$]" | fmt -w 2500)
    WIFI_SSID_ARRAY=($WIFI_SSID_ARRAY)
    echo -e $(print_wifi_ssid)
    read -p "Press 'Enter' to continue: "
    continue
  elif [[ "$option" == 6 ]]; then
    # get array wifi ssid (configs)
    WIFI_SSID_ARRAY=$(ls -w 1 "$CONFIG_PATH" | grep -oi ".*[^.conf$]" | fmt -w 2500)
    WIFI_SSID_ARRAY=($WIFI_SSID_ARRAY)

    # print configs and selection will be deleted config file
    while true; do
      echo -e $(print_wifi_ssid)
      read -p "Select file for delete: " SELECT_CONFIG_FILE

      if [[ "$SELECT_CONFIG_FILE" == [0-9] ]]; then
        CONFIG_FILE=${WIFI_SSID_ARRAY["$SELECT_CONFIG_FILE"]}
      else
        $SETCOLOR_FAILURE
        echo "This wifi ssid is not =("
        $SETCOLOR_NORMAL
        sleep $DELAY
        continue
      fi

      if [[ "$CONFIG_FILE" == "" ]]; then
        $SETCOLOR_FAILURE
        echo "This wifi ssid is not =("
        $SETCOLOR_NORMAL
        sleep $DELAY
        continue
      fi

      break
    done

    # You really want delete this file?
    CONFIG_FILE_PATH="$CONFIG_PATH${CONFIG_FILE}.conf"
    read -p "You want delete ${CONFIG_FILE_PATH}? [y/n]: " IS_DELETE

    if [[ "$IS_DELETE" == "y" ]]; then
      cd "$CONFIG_PATH" &&
      rm "${CONFIG_FILE}.conf" &&

      $SETCOLOR_SUCCESS
      echo "File will be deleted"
      $SETCOLOR_NORMAL
      sleep $DELAY
      continue
    else
      $SETCOLOR_FAILURE
      echo "File will be not deleted =3"
      $SETCOLOR_NORMAL
      sleep $DELAY
      continue
    fi
  else
    $SETCOLOR_FAILURE
    echo "This option is not =("
    $SETCOLOR_NORMAL
    sleep $DELAY
    continue
  fi
done
